package com.example.librarymanagementproject2.mq;

import com.example.librarymanagementproject2.Common.config.RabbitConfig;
import com.example.librarymanagementproject2.Common.message.UserOperationMessage;
import com.example.librarymanagementproject2.converter.UserConverter;
import com.example.librarymanagementproject2.mapper.UserMapper;
import com.example.librarymanagementproject2.model.document.UserDocument;
import com.example.librarymanagementproject2.model.entity.User;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.stereotype.Component;

/**
 * @ClassName UserConsumer
 * @Description TODO
 * @Author Kx
 * @Date 2025/10/27 11:07
 * @Version 1.0
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class UserConsumer {
    private final ElasticsearchOperations elasticsearchOperations;
    private final UserMapper userMapper;
    private final UserConverter userConverter;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = RabbitConfig.USER_OPERATION_QUEUE, durable = "true"),
            exchange = @Exchange(name = RabbitConfig.USER_EXCHANGE, type = ExchangeTypes.DIRECT),
            key = RabbitConfig.USER_OPERATION_ROUTING_KEY
    ))
    public void receive(UserOperationMessage userOperationMessage) {
        switch (userOperationMessage.getOperationType()) {
            // 修改密码同步索引
            case "updatePassword":
                // 修改用户文档信息
                updateUserDocument(userOperationMessage);
                log.info("修改密码同步索引成功");
                break;
            // 修改用户名同步索引
            case "updateUsername":
                // 修改用户文档信息
                updateUserDocument(userOperationMessage);
                log.info("修改用户名同步索引成功");
                break;
            // 注销账户同步索引
            case "cancelAccount":
                // 获取用户id
                Long userId = userOperationMessage.getId();

                // 删除文档信息
                elasticsearchOperations.delete(userId.toString(), UserDocument.class);
                log.info("注销账户同步索引成功");
                break;
        }
    }

    /**
     * @author: kx
     * @date: 2025/10/27 11:47
     * @param userOperationMessage : 用户操作信息
     * @description: 修改用户文档信息
    */
    private void updateUserDocument(UserOperationMessage userOperationMessage) {
        // 获取用户id
        Long userId = userOperationMessage.getId();

        // 获取用户信息
        User user = userMapper.getUserById(userId);

        // 转换为用户文档信息
        UserDocument userDocument = userConverter.toUserDocument(user);

        // 修改文档信息
        elasticsearchOperations.update(userDocument);
    }
}
